Ext.ns('AIR2.Source.Contact');
/***************
 * Source Contact Modal - Phone tab
 *
 * @class AIR2.Source.Contact.Phone
 * @extends Ext.Container
 * @xtype air2srcconphone
 * @cfg {String} srcUrl
 *
 */
AIR2.Source.Contact.Phone = function(cfg) {
    // headers and add button
    var header = new Ext.BoxComponent({
        cls: 'header',
        html: '<table class="phone-row"><tr>' +
                '<td class="primary">Primary</td>' +
                '<td class="type">Type</td>' +
                '<td class="cntry">Country</td>' +
                '<td class="numb">Number</td>' +
                '<td class="xten">Ext</td>' +
                '<td class="ops"></td>' +
            '</tr></table>',
        listeners: {
            // create an add-new button
            afterrender: function() {
                new AIR2.UI.Button({
                    air2type: 'BLUE',
                    iconCls: 'air2-icon-add',
                    text: 'New',
                    renderTo: header.el.child('.ops', true),
                    scope: livedv,
                    handler: function() {
                        var rec = new this.store.recordType();
                        this.store.insert(0, rec);
                        this.createEditor(rec, this.getNode(rec));
                    }
                });
            }
        }
    });

    // content
    var livedv = new AIR2.UI.LiveEditor({
        url: cfg.srcUrl + '/phone',
        sortField: 'sph_cre_dtim',
        sortDir: 'DESC',
        pageSize: 50,
        hideButtons: true,
        allowEdit: AIR2.Source.SUMMDATA.authz.may_write,
        allowDelete: AIR2.Source.SUMMDATA.authz.may_write,
        tpl: new Ext.XTemplate(
            '<table class="phone-row"><tr>' +
              '<td class="primary">{[AIR2.Format.bool(values.sph_primary_flag)]}</td>' +
              '<td class="type">{[this.phoneType(values)]}</td>' +
              '<td class="cntry">{sph_country}</td>' +
              '<td class="numb">{sph_number}</td>' +
              '<td class="xten">{sph_ext}</td>' +
              '<td class="ops">' +
                '<button class="air2-rowedit"></button>' +
                '<button class="air2-rowdelete"></button>' +
              '</td>' +
            '</tr></table>',
            {
                compiled: true,
                disableFormats: true,
                phoneType: function(val) {
                    var code = val.sph_context;
                    var types = {
                        H: 'Home',
                        W: 'Work',
                        C: 'Cell',
                        O: 'Other'
                    };
                    return (types[code]) ? types[code] : '';
                }
            }
        ),
        editForm: {
            layout:'hbox',
            items: [{
                xtype: 'checkbox',
                name: 'sph_primary_flag',
                margins: '4px 7px -4px 12px',
                width: 42
            },{
                xtype: 'air2combo',
                name: 'sph_context',
                choices: [
                    ['H', 'Home'],
                    ['W', 'Work'],
                    ['C', 'Cell'],
                    ['O', 'Other']
                ],
                margins: '0 14px 0 0',
                width: 64
            },{
                xtype: 'textfield',
                name: 'sph_country',
                autoCreate: {tag: 'input', type: 'text', autocomplete: 'off', maxlength: '3'},
                margins: '0 28px 0 0',
                width: 50
            },{
                xtype: 'textfield',
                name: 'sph_number',
                autoCreate: {tag: 'input', type: 'text', autocomplete: 'off', maxlength: '12'},
                margins: '0 18px 0 0',
                width: 100
            },{
                xtype: 'textfield',
                name: 'sph_ext',
                autoCreate: {tag: 'input', type: 'text', autocomplete: 'off', maxlength: '12'},
                width: 50
            }],
            listeners: {
                beforeedit: function(fp, rec) {
                    if (rec.phantom && rec.store.getCount() == 1) {
                        // set initial primary == true
                        fp.getForm().findField('sph_primary_flag').setValue(true);
                    }

                },
                aftersave: function(fp, savedrec) {
                    if (savedrec.data.sph_primary_flag) {
                        // unset other primary flags
                        savedrec.store.each(function(r) {
                            if (r != savedrec) {
                                r.data.sph_primary_flag = false; //no events
                                var idx = livedv.indexOf(r);
                                livedv.refreshNode(idx);
                            }
                        });
                    }
                }
            }
        }
    });

    // call parent constructor
    cfg.items = [header, livedv];
    AIR2.Source.Contact.Phone.superclass.constructor.call(this, cfg);
}
Ext.extend(AIR2.Source.Contact.Phone, Ext.Container, {
    cls: 'air2-source-contact-phone'
});
Ext.reg('air2srcconphone', AIR2.Source.Contact.Phone);
